package com.example.administrator.mymvpapplication.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.example.administrator.mymvpapplication.R;
import com.example.administrator.mymvpapplication.di.component.DaggerMainComponent;
import com.example.administrator.mymvpapplication.di.module.MainModule;
import com.example.administrator.mymvpapplication.model.User;
import com.example.administrator.mymvpapplication.presenter.impl.MainPresenterImpl;

import javax.inject.Inject;


public class MainActivity extends BaseActivity implements MainBaseView {
    String idea = "MVP模式拥有标准与规范，可以使用在大型项目上，可以使用规范更好的扩展与维护，" +
            "View与Model分离，使用Presenter作为中间人操作，在绑定后注意解绑，可以预防在多线程中应为View" +
            "销毁出现的数据回传后找不到View出现空指针问题";
    //被Inject注解的，不能使用private修饰
    @Inject
    public MainPresenterImpl mainPresenter;

    private EditText mET_account;
    private EditText mET_password;
    private TextView mTV_idea;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mTV_idea.setText(idea);
    }

    private void initView() {
        mTV_idea = (TextView) this.findViewById(R.id.mTV_idea);
        /*初始化控件得到帐号密码*/
        mET_account = (EditText) this.findViewById(R.id.mET_account);
        mET_password = (EditText) this.findViewById(R.id.mET_password);
        Button mBT_login = (Button) this.findViewById(R.id.mBT_login);

        mET_account.setText("abc");
        mET_password.setText("123");

        /*使用Presenter绑定view与model*/

        /*常规实例化MainPresenterImpl*/
        //mainPresenter = new MainPresenterImpl(this);

        //MVP+Dagger2，Dagger2注入方法，实例化MainPresenterImpl
        DaggerMainComponent.builder().mainModule(new MainModule(MainActivity.this)).build().inject(this);
        mainPresenter.bindView(MainActivity.this);

        mBT_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String assount = mET_account.getText().toString().trim();
                String passwork = mET_password.getText().toString().trim();
                if (!TextUtils.isEmpty(assount) && !TextUtils.isEmpty(passwork)) {
                    User user = new User();
                    user.account = assount;
                    user.passwork = passwork;
                    /*调用Presenter的父类执行逻辑判定*/
                    mainPresenter.loading(user);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mainPresenter != null) {
            mainPresenter.unBindView();
        }
    }


    /*调用Presenter执行逻辑判定后回调的结果*/
    @Override
    public void loginSuccess() {
        showToast("登陆成功");
    }

    /*调用Presenter执行逻辑判定后回调的结果*/
    @Override
    public void loginFail() {
        showToast("登录失败");
    }
}
